<?php

/**
 * Main controller
 *
 * @uses       Beaver_Controller_Action
 * @package    Beaver
 * @subpackage Controller
 */
class Beaver_IndexController extends Beaver_Controller_Action
{
	public function coreAction()
	{
		$this->setResponseToJavascript();
	}
    
	public function indexAction()
	{
		$this->setResponseToHtml();
		
		$this->view->headLink()->headLink( array(
			"rel"   => "stylesheet",
			"media" => "screen",
			"type"  => "text/css",
			"href"  => "/extjs/resources/css/ext-all.css"
		));
		
		$this->view->headLink()->headLink( array(
			"rel"   => "stylesheet",
			"media" => "screen",
			"type"  => "text/css",
			"href"  => "/css/application.css"
		));
		
		$this->view->headScript()->appendFile(
    		"/jquery/jquery.js",
    		"text/javascript"
		);
		
		$this->view->headScript()->appendFile(
    		"/extjs/adapter/jquery/ext-jquery-adapter-debug.js",
    		"text/javascript"
		);
			
		$this->view->headScript()->appendFile(
    		"/extjs/ext-all-debug.js",
    		"text/javascript"
		);

		$this->view->headScript()->appendFile(
    		"/beaver/index/get-ext-api",
    		"text/javascript"
		);
		
		$this->view->headScript()->appendFile(
    		"/beaver/index/core",
    		"text/javascript"
		);
		
		$this->view->headScript()->appendFile(
    		"/js/application.js",
    		"text/javascript"
		);
		
		$this->view->headTitle('Beaver');
		
		$this->_layout->setLayout('main');
		
	}
	
	public function getExtApiAction()
	{
		$this->setResponseToJavascript();

                
                // retrieve the Ext.Direct config file
		$extDirectConfig = $this->getInvokeArg("bootstrap")->getOptions();

                // if file did not exist when Zend_Application started, now it does
                if(!isset($extDirectConfig["ExtDirect"])){
                    $extDirectConfig = new Zend_Config_Ini($extDirectConfig['directIniPath'], APPLICATION_ENV);
                    $extDirectConfig = $extDirectConfig->toArray();
                }
                
		$apiRemote = new stdClass();
		$apiRemote->url       = "/beaver/index/ext-direct-routing";
		$apiRemote->type      = "remoting";
		$apiRemote->namespace = "Beaver.Remote";
		$apiRemote->actions   = new stdClass();
		foreach( $extDirectConfig["ExtDirect"]["Remote"] as $routes => $route )
		{
			$attributesLength = 1;
			if ( isset($route["attributes"]) ) $attributesLength = (int) $route["attributes"];
			$action = array();
			$action["name"]        = $route["action"];
			$action["len"]         = $attributesLength;
			if ( isset( $route["formHandler"] ) && $route["formHandler"] )
			{
				$action["formHandler"] = true;
			}
			
			$controller = $route["controller"];
			
			if ( !isset( $apiRemote->actions->$controller ) )
			{
				$apiRemote->actions->$controller = array($action);
			}
			else
			{
				array_push( $apiRemote->actions->$controller, $action );
			}
		}
		
		$this->view->routes = $apiRemote;
		
		$this->_layout->setLayout('empty');
	}
	
	public function extDirectRoutingAction()
	{
		$this->setResponseToJson();
		$params = $this->_request->getParam("extRequest");
		if ( count($params) > 1 )
		{
			echo "[";
		}
		//foreach($params as $request)
		for($i = 0; $i < count($params); $i++)
		{
			$viewExtra = "";
			if ( count($params) > 1 && $i != 0 )
			{
				$viewExtra.= ",";
			}
			
			if ( count($params) > 1 && $i == 0 )
			{
				$viewExtra.= "]";
			}
		
			$this->_helper->actionStack(
				strtolower($params[$i]["method"]),
				strtolower($params[$i]["action"]),
				"default",
				array(
					"extData"   => $params[$i],
					"viewExtra" => $viewExtra,
					"extParams" => $params[$i]["data"]
				)
			);
		}
	}
}
